/**
 * 创建 meunCreate 云函数，实现微信自定义菜单
 */
function onRequest (request, response, modules) {
  
    var db = modules.oData;
    var openid = request.body.openid || 'oOatmwMby6XL74qB5tIG3s1BCns4';
    var content = request.body.content || 'Hello World';
    //查询数据库公众号access_token
    db.findOne({
      "table": "access_token",
      "objectId": "V8rwWWWd"
    }, function (err, data) {
  
      var result = JSON.parse(data);
      var access_token = result.access_token;
      if (access_token) {
        sendMessageWeixin(openid, access_token);
      }
      //  response.send(result.access_token);
    });
  
    // 创建菜单函数
    function sendMessageWeixin (openid, token) {
      var http = modules.oHttp;
      var contentJson = {
        "button": [
          {
            "name": "教务查询",
            "sub_button": [
              {
                "type": "view",
                "name": "查课表",
                "url": "http://www.qq.com"
              },
              {
                "type": "view",
                "name": "查成绩",
                "url": "http://www.qq.com"
              },
              {
                "type": "view",
                "name": "空课室",
                "url": "http://www.qq.com"
              }
            ]
          },
          {
            "name": "综合",
            "sub_button": [
              {
                "type": "view",
                "name": "追剧",
                "url": "http://neets.cc/?_k=xhqz"
              }
            ]
          }]
      };
  
  
      /**
  *发起 Post请求
  */
      //获取 Http 模块
      var http = modules.oHttp;
  
      var options = {
        "url": 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' + token,
  
        "body": JSON.stringify(contentJson)
      };
      http.post(options, function (error, res, body) {
        response.send(body);
      });
    }
  
  }                                                                                    